package com.loong.shop.handler;

import com.google.common.collect.Lists;
import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.business.GoodsSpecificationValueBusiness;
import com.loong.shop.common.backend.param.GoodsSizeValueParam;
import com.loong.shop.common.backend.param.GoodsSizeValueQueryParam;
import com.loong.shop.common.backend.result.GoodsSizeValueResult;
import com.loong.shop.common.forehead.param.GoodsSizeValueQueryForeheadParam;
import com.loong.shop.common.forehead.result.GoodsSizeValueForeheadResult;
import com.loong.shop.model.GoodsSku;
import com.loong.shop.model.GoodsSpecificationValue;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理商品规格值的业务的handler
 * @Author: 刘群
 * @Date: 2020/10/27 8:56
 */
@Component
public class GoodsSpecificationValueHandler {

    @Resource
    private GoodsSpecificationValueBusiness valueBusiness;

    @Resource
    private GoodsSkuHandler skuHandler;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 后台新增商品规格值
     * @param param 商品规格值封装的参数
     */
    public void saveGoodsSpecificationValue(GoodsSizeValueParam param){
        //先判断是否存在重复的规格值，若存在则不能新增，若不存在则可以新增
        GoodsSizeValueQueryParam queryParam = GoodsSizeValueQueryParam.builder()
                .sizeNameId(param.getGoodsSpecificationId())
                .sizeValue(param.getGoodsSpecificationValue())
                .build();
        List<GoodsSpecificationValue> valueList = valueBusiness.findDuplicationValueByParam(queryParam);
        if (CollectionUtils.isEmpty(valueList)){
            //不存在则新增
            param.setCreateTime(new Date());
            valueBusiness.save(TransformUtils.transform(param, GoodsSpecificationValue.class));
        }else{
            throw new GlobalException(ResultCode.PARAM_ERROR,"specification value already exists");
        }
    }

    /**
     * 后台更新商品规格值
     * @param param 商品规格值封装的参数
     */
    public void updateGoodsSpecificationValue(GoodsSizeValueParam param){
        //直接更新
        param.setLastUpdateTime(new Date());
        valueBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, GoodsSpecificationValue.class));

    }

    /**
     * 后台批量删除商品规格值
     * @param idList 商品规格值id集合
     */
    public void deleteGoodsSpecificationValue(List<Long> idList){
        ArrayList<Long> ids = Lists.newArrayList();
        batchExecutor.batchExecute(idList,id->id.toString(),(item)->{
            List<GoodsSku> goodsSkuList = skuHandler.findGoodsSpecificationById(item);
            if (CollectionUtils.isEmpty(goodsSkuList)){
                ids.add(item);
            }else {
                throw new GlobalException("specification value is used");
            }
        });
        valueBusiness.deleteByIds(StringUtils.join(ids,","));

    }

    /**
     * 后台分页查询商品规格值
     * @param query 查询参数
     * @return 分页结果
     */
    public Pager<GoodsSizeValueResult> pageQueryGoodsSpecificationValue(BaseQuery query){
        Pager<GoodsSpecificationValue> pager = valueBusiness.pageQuery(query, () -> valueBusiness.findAll());
        return TransformUtils.transformPage(pager, GoodsSizeValueResult.class);
    }

    /**
     * 后台根据参数查询商品规格值
     * @param sizeNameId 商品规格名称id
     * @return 商品规格值集合
     */
    public List<GoodsSizeValueResult> findGoodsSizeValueById(Long sizeNameId){
         return TransformUtils.transformList(valueBusiness.findSizeValueByParam(sizeNameId,null),GoodsSizeValueResult.class);
    }

    /**
     * 前台根据参数查询商品规格值
     * @param param 查询参数
     * @return 商品规格值集合
     */
    public List<GoodsSizeValueForeheadResult> findGoodsSizeValueToForehead(GoodsSizeValueQueryForeheadParam param){
        List<GoodsSpecificationValue> valueList = valueBusiness.findSizeValueByParam(param.getGoodsSpecificationId(), param.getGoodsSpecificationValue());
        return TransformUtils.transformList(valueList,GoodsSizeValueForeheadResult.class);
    }
}
